package com.airlines.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author xxxx
 * @date 2017年9月28日
 * @description 由于在 Mybatis监听器中，是优先于Spring加载的，无法直接注入
 *              Sring的服务类，故创建一个获取Spring容器类的工具类
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
	private static ApplicationContext applicationContext = null;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if(SpringContextUtil.applicationContext == null) {
			SpringContextUtil.applicationContext = applicationContext;
		}
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static Object getBean(String beanName) {
		Object o =  applicationContext.getBean(beanName);
		return o;
	}

	public static Object getBean(Class<?> c) {
		Object o = applicationContext.getBean(c);
		return o;
	}
}
